/**
 * JSparrow - Une version numérique du jeu "Attention Pirates !" - Copyright (C) 2014 Black Pearl
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 */
package App.Vue;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class Panneau_ParametragePartie extends JPanel_Principal{
	
	//Elements graphiques
	private JButton validerBouton;
	private JButton retourBouton;
	public JTextField champNomPartie;
	private JLabel labelNomPartie;
	private JLabel labelNbJoueur;
	private JSpinner spinnerNbJoueur;
	private JPanel panelInterieur;
	private Image parchemin;
	private boolean flagInit = true;
	//Layout container
	private GridBagConstraints gbc;
	protected Panneau_CreationPartie partie;
	
	public Panneau_ParametragePartie(Panneau_CreationPartie partie){
		super();
		this.partie = partie;
		this.panelInterieur = new JPanel();
		this.validerBouton = new JButton("Valider");
		this.retourBouton = new JButton("Retour");
		this.champNomPartie = new JTextField();
		this.labelNomPartie = new JLabel("Nom de la partie");
		this.labelNbJoueur = new JLabel("Nombre de joueur");
		this.panelInterieur = new JPanel();
		SpinnerModel SM = new SpinnerNumberModel(2,2,6,1);
		this.spinnerNbJoueur = new JSpinner(SM);
		
		validerBouton.addActionListener(new boutonListener("Valider"));
		retourBouton.addActionListener(new boutonListener("Retour"));
		//Layout des Jpanel
		this.gbc = new GridBagConstraints();
		this.setLayout(new GridBagLayout());
		this.panelInterieur.setLayout(new GridLayout(3, 2,10,10));   
		
		//Reglage de la transparence des fonds
		this.setBackground(TRANSPARENT);
		this.panelInterieur.setBackground(TRANSPARENT);
		this.panelInterieur.setOpaque(false);
		this.champNomPartie.setOpaque(false);
		this.spinnerNbJoueur.setOpaque(false);
		this.spinnerNbJoueur.setOpaque(false);
		this.spinnerNbJoueur.getEditor().setOpaque(false);
		((JSpinner.NumberEditor)this.spinnerNbJoueur.getEditor()).getTextField().setOpaque(false);
		
		//Reglage de la transparence des bordures
		this.champNomPartie.setBorder(javax.swing.BorderFactory.createEmptyBorder());
		this.spinnerNbJoueur.setBorder(javax.swing.BorderFactory.createEmptyBorder());
		
		//Ajout de marges au panel interieur
		this.panelInterieur.setBorder(new EmptyBorder(50, 50, 20, 50) );
		//this.panelInterieur.setPreferredSize(new Dimension(400,150));
		
		//Font
		this.labelNomPartie.setFont(this.texteFont);
		this.labelNbJoueur.setFont(this.texteFont);
		this.spinnerNbJoueur.setFont(this.texteFont);
		this.champNomPartie.setFont(this.texteFont);
		this.validerBouton.setFont(this.texteFont);
		this.retourBouton.setFont(this.texteFont);
		
		//Ajout des elements graphiques au panel interieur
		this.panelInterieur.add(labelNomPartie);
		this.panelInterieur.add(champNomPartie);
		this.panelInterieur.add(labelNbJoueur);
		this.panelInterieur.add(spinnerNbJoueur);
		this.panelInterieur.add(validerBouton);
		this.panelInterieur.add(retourBouton);
		
		
		
		//Position de panel interieur au milieu
	    gbc.gridy=1;
	    gbc.gridx=1;
	    gbc.gridwidth = 1;
	    gbc.gridheight = 1;
	    this.add(panelInterieur,gbc);    
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if(flagInit){
			Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
			try {
				BufferedImage bufferedFond = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/parcheminH.png"));
				parchemin = bufferedFond.getScaledInstance(panelInterieur.getWidth(),panelInterieur.getHeight(), 0);

			} catch (IOException e) {
				e.printStackTrace();
			}
			flagInit = false;
		}
		g.drawImage(parchemin, 0, 0, this);
	}

	class boutonListener implements ActionListener{

		String bouton;
		public boutonListener(String bouton){
			super();
			this.bouton = bouton;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			//TODO : Notification au controleur
			switch (bouton) {
			case "Valider" ://Reinitialise la disposition
				partie.validerCreerPartie(champNomPartie.getText(), (int)spinnerNbJoueur.getValue());
				break;
			case "Retour":
				partie.retourMenu();
				break;
			default :
			}
		}
	}
	
	public void valider_clic(){
		
	}
	
	public void retour_clic(){
		
	}
}
